package com.emotion.recognition.client;

import com.emotion.recognition.shared.SOMConfig;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class SOMConfigurator extends Composite {

    private static SOMConfiguratorUiBinder uiBinder = GWT.create(SOMConfiguratorUiBinder.class);

    interface SOMConfiguratorUiBinder extends UiBinder<Widget, SOMConfigurator> {
    }

    public SOMConfigurator() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiField
    TextBox learningRate;

    @UiField
    TextBox radius;

    @UiField
    TextBox latticeSize;

    @UiField
    TextBox maxEpochs;

    public SOMConfigurator(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));

        makeIntegerOnly(latticeSize);
        makeDecimalOnly(learningRate);
        makeDecimalOnly(radius);
        makeIntegerOnly(maxEpochs);
    }

    public SOMConfig getValue() {
        SOMConfig som = new SOMConfig();
        som.latticeSize = Integer.valueOf(latticeSize.getValue());
        som.radius = Double.valueOf(radius.getValue());
        som.learningRate = Double.valueOf(learningRate.getValue());
        som.maxEpochs = Integer.valueOf(maxEpochs.getValue());
        return som;
    }

    public void makeIntegerOnly(final TextBox tb) {
        tb.addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent event) {
                char keyCode = event.getCharCode();

                if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyCodes.KEY_TAB)
                        && (keyCode != (char) KeyCodes.KEY_BACKSPACE)
                        && (keyCode != (char) KeyCodes.KEY_DELETE)
                        && (keyCode != (char) KeyCodes.KEY_ENTER)
                        && (keyCode != (char) KeyCodes.KEY_HOME)
                        && (keyCode != (char) KeyCodes.KEY_END)
                        && (keyCode != (char) KeyCodes.KEY_LEFT)
                        && (keyCode != (char) KeyCodes.KEY_UP)
                        && (keyCode != (char) KeyCodes.KEY_RIGHT)
                        && (keyCode != (char) KeyCodes.KEY_DOWN)) {
                    // TextBox.cancelKey() suppresses the current keyboard event.
                    tb.cancelKey();
                }
            }
        });
    }

    public void makeDecimalOnly(final TextBox tb) {
        tb.addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent event) {
                char keyCode = event.getCharCode();

                if ((!Character.isDigit(keyCode)) && (keyCode != '.')
                        && (keyCode != (char) KeyCodes.KEY_TAB)
                        && (keyCode != (char) KeyCodes.KEY_BACKSPACE)
                        && (keyCode != (char) KeyCodes.KEY_DELETE)
                        && (keyCode != (char) KeyCodes.KEY_ENTER)
                        && (keyCode != (char) KeyCodes.KEY_HOME)
                        && (keyCode != (char) KeyCodes.KEY_END)
                        && (keyCode != (char) KeyCodes.KEY_LEFT)
                        && (keyCode != (char) KeyCodes.KEY_UP)
                        && (keyCode != (char) KeyCodes.KEY_RIGHT)
                        && (keyCode != (char) KeyCodes.KEY_DOWN)) {
                    // TextBox.cancelKey() suppresses the current keyboard event.
                    tb.cancelKey();
                }
            }
        });
    }
}
